<template>
  <view class="mShare">
    <u-popup
      mode="bottom"
      v-model="show"
      :mask-close-able="false"
      :duration="0"
    >
      <view class="dis-list">
        <!-- #ifdef MP-WEIXIN -->
        <view>
          <button class="button" hover-class="none" open-type="share">
            <image :src="asyncImgs.goods.share_wx" mode=""> </image>
            <view>
              {{ $t("common.shareFriend") }}
            </view>
          </button>
        </view>
        <!-- #endif -->
        <view @click="generate">
          <image :src="asyncImgs.goods.share_qrcode" mode=""> </image>
          <view>{{ $t("common.beforePoster") }}</view>
        </view>
      </view>
      <view class="close" @click="show = false">{{ $t("common.close") }}</view>
    </u-popup>

    <view class="poster" v-if="poster_show">
      <view class="poster-content">
        <view class="poster-close">
          <image
            :src="asyncImgs.mch.close"
            mode=""
            @click="poster_show = !poster_show"
          ></image>
        </view>
        <view class="poster-img-content">
          <image
            v-if="imgshow"
            class="poster-img"
            :src="img_src"
            mode="aspectFill"
            @click="previewImg"
          ></image>
          <view v-if="!imgshow" class="poster-load">
            <image :src="asyncImgs.home.qrcode_close" mode=""></image>
            <view>{{ $t("common.poster") }}</view>
          </view>
        </view>
        <view class="save" v-if="imgshow">
          <!-- #ifdef MP-WEIXIN -->
          <view v-if="openSettingBtnHidden" class="save-one" @click="saveEwm">
            {{ $t("common.album") }}
          </view>

          <button
            v-else
            class="purple_btn btn_box"
            hover-class="none"
            open-type="openSetting"
            @opensetting="handleSetting"
          >
            {{ $t("common.album") }}
          </button>
          <!-- <view class="save-one">保存图片</view> -->
          <view class="save-two">{{ $t("common.shareAlbum") }}</view>
          <!-- #endif -->
          <!--  #ifdef  APP-PLUS -->
          <u-row gutter="12" style="padding: 20rpx">
            <u-col
              span="6"
              style="margin-bottom: 40rpx; text-align: center"
              @click="shareTo('WXSceneSession')"
            >
              <image
                :src="asyncImgs.goods.share_wx"
                mode="widthFix"
                style="width: 100rpx; height: 100rpx"
              ></image>
              <view style="font-size: 32rpx">{{ $t("common.shareWx") }}</view>
            </u-col>
            <u-col
              span="6"
              style="margin-bottom: 40rpx; text-align: center"
              @click="shareTo('WXSenceTimeline')"
            >
              <image
                :src="asyncImgs.goods.wxtimeline"
                mode="widthFix"
                style="width: 100rpx; height: 100rpx"
              ></image>
              <view style="font-size: 32rpx">{{
                $t("common.shareCircle")
              }}</view>
            </u-col>
          </u-row>
          <!-- #endif -->
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      show: false,
      poster_show: false,
      imgshow: false,
      img_src: "",
      openSettingBtnHidden: true, //是否授权
    };
  },
  props: {
    topic_id: {
      type: String,
      default: "",
    },
  },
  methods: {
    shareTo(type) {
      switch (type) {
        case "WXSceneSession":
          uni.share({
            provider: "weixin",
            scene: "WXSceneSession",
            type: 2,
            imageUrl: this.img_src,
          });
          break;
        case "WXSenceTimeline":
          uni.share({
            provider: "weixin",
            scene: "WXSenceTimeline",
            type: 2,
            imageUrl: this.img_src,
          });
          break;
        default:
          break;
      }
    },
    //微信小程序保存到相册
    handleSetting(e) {
      if (!e.detail.authSetting["scope.writePhotosAlbum"]) {
        this.openSettingBtnHidden = false;
      } else {
        this.openSettingBtnHidden = true;
      }
    },
    //获取授权
    saveEwm(e) {
      var _self = this;
      //获取相册授权
      uni.getSetting({
        success(res) {
          if (!res.authSetting["scope.writePhotosAlbum"]) {
            uni.authorize({
              scope: "scope.writePhotosAlbum",
              success() {
                //这里是用户同意授权后的回调
                _self.saveImgToLocal();
              },
              fail() {
                //这里是用户拒绝授权后的回调
                _self.openSettingBtnHidden = false;
              },
            });
          } else {
            //用户已经授权过了
            _self.saveImgToLocal();
          }
        },
      });
    },
    //下载并保存图片
    saveImgToLocal(e) {
      var _self = this;
      uni.showModal({
        title: _self.$t("order.prompt"),
        content: _self.$t("common.shareContent"),
        success: function (res) {
          if (res.confirm) {
            uni.downloadFile({
              url: _self.img_src, //图片地址
              success: (res) => {
                if (res.statusCode === 200) {
                  uni.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function () {
                      uni.showToast({
                        title: _self.$t("common.saveSuc"),
                        icon: "none",
                      });
                    },
                    fail: function () {
                      uni.showToast({
                        title: _self.$t("common.saveFail"),
                        icon: "none",
                      });
                    },
                  });
                }
              },
            });
          } else if (res.cancel) {
          }
        },
      });
    },
    showShare() {
      this.show = true;
    },
    //生成海报
    generate() {
      this.show = false;
      this.poster_show = true;
      var params = {
        goods_id: this.topic_id,
      };
      this.$allrequest.special
        .topicQrcode(params, true)
        .then((res) => {
          if (res.code == 0) {
            this.img_src = res.data.pic_url;
            this.imgshow = true;
          }
        })
        .catch((error) => {
          this.$u.toast(this.$t("common.posterFail"));
          this.poster_show = false;
        });
    },
    // 点击预览图片
    previewImg() {
      uni.previewImage({
        current: "0",
        urls: [this.img_src],
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.dis-list {
  display: flex;
  justify-content: space-around;
  background-color: #f2f2f2;

  > view {
    padding: 60rpx 0rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    image {
      width: 120rpx;
      height: 120rpx;
      margin-bottom: 12rpx;
      border-radius: 50%;
    }

    view {
      text-align: center;
      width: 180rpx;
      height: 80rpx;
      line-height: 80rpx;
      font-size: 32rpx;
    }

    button {
      position: relative;
      display: block;
      margin-left: auto;
      margin-right: auto;
      padding: 0rpx;
      box-sizing: border-box;
      text-align: center;
      text-decoration: none;
      border-radius: 5px;
      width: 180rpx;
      height: 80rpx;
      -webkit-tap-highlight-color: transparent;
      overflow: hidden;
      color: #000000;
      background-color: #f2f2f2;
      font-size: 32rpx;
    }

    // 边框样式
    button::after {
      border: 0;
    }

    // 禁用时样式
    button[disabled] {
      background-color: #fff;
      color: #666;
    }

    // 点击时样式
    .button-hover {
      color: rgba(0, 0, 0, 0.6);
      background-color: #fff;
    }

    // button::after {
    // 	border: none;
    // }
  }
}

.close {
  text-align: center;
  height: 100rpx;
  line-height: 100rpx;
  background-color: #ffffff;
}

.poster {
  position: fixed;
  top: 0rpx;
  left: 0rpx;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40rpx;

  .poster-content {
    width: 100%;
    height: 100%;
    border-radius: 10rpx;
    background-color: #ffffff;
    box-sizing: border-box;
    padding: 24rpx;
    display: flex;
    flex-direction: column;
    align-items: center;

    // justify-content: center;
    .poster-close {
      width: 100%;
      display: flex;
      justify-content: flex-end;

      image {
        width: 40rpx;
        height: 40rpx;
      }
    }

    .poster-img-content {
      margin-bottom: 36rpx;
      width: 80%;
      height: 75%;

      .poster-img {
        margin-top: 36rpx;
        width: 100%;
        height: 100%;
      }

      .poster-load {
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;

        image {
          width: 88rpx;
          height: 88rpx;
        }

        view {
          margin-top: 10rpx;
          color: #888;
        }
      }
    }

    .save {
      padding-top: 36rpx;
      width: 80%;

      .save-one {
        width: 100%;
        margin-bottom: 11rpx;
        height: 80rpx;
        line-height: 80rpx;
        text-align: center;
        border: 1rpx solid #f3f3f3;
      }

      .save-two {
        color: #888;
        font-size: 24rpx;
        text-align: center;
      }
    }
  }
}

.mShare {
  .button {
    height: auto;
    color: #333;
  }

  image {
    display: block;
    margin: 0 auto;
  }
}
</style>
